﻿using Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicTier.DAL.DataContext;
using BusinessLogicTier.Connection;

namespace BusinessLogicTier.DAL
{
    public class NguoiDung_DAL
    {
        /// <summary>
        /// Xuannt 5/12/2013
        /// </summary>
        private static Log _logFile = new Log();
        private static Utils util = new Utils();
        //Thêm thông tin người dùng
        public static bool Insert(tbl_NguoiDung objInsert)
        {
            try
            {
                var db = new QLBHDataContext(DataService.m_ConnectString);
                db.tbl_NguoiDungs.InsertOnSubmit(objInsert);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                _logFile.Write("Exception when using NguoiDung_DAL.cs => Insert Message:" + ex.Message + " Source:" + ex.Source + " StackTrace:" + ex.StackTrace);
                return false;
            }
        }

        //Sửa thông tin người dùng
        public static bool Update(tbl_NguoiDung objUpdate)
        {
            tbl_NguoiDung objTemp = new tbl_NguoiDung();
            var db = new QLBHDataContext(DataService.m_ConnectString);
            try
            {
                objTemp = (from kh in db.tbl_NguoiDungs
                           where kh.TrangThai == true && kh.ID == objUpdate.ID
                           select kh).FirstOrDefault();
                if (objTemp != null)
                {
                    objTemp.ID = objUpdate.ID;
                    objTemp.DienGiai = objUpdate.DienGiai;
                    objTemp.fk_NhanVien = objUpdate.fk_NhanVien;
                    objTemp.fk_VaiTro = objUpdate.fk_VaiTro;
                    objTemp.MatKhau = objUpdate.MatKhau;
                    objTemp.QuanLy = objUpdate.QuanLy;
                    objTemp.TaiKhoan = objUpdate.TaiKhoan;
                    objTemp.NguoiSua = util.GetUserID();
                    objTemp.NgaySua = DateTime.Now;
                    objTemp.TrangThai = objUpdate.TrangThai;
                }
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                _logFile.Write("Exception when using NguoiDung_DAL.cs => Update Message:" + ex.Message + " Source:" + ex.Source + " StackTrace:" + ex.StackTrace);
                return false;
            }
        }

        //Xóa thông tin người dùng
        public static bool Delete(tbl_NguoiDung objDelete)
        {
            try
            {
                var db = new QLBHDataContext(DataService.m_ConnectString);
                tbl_NguoiDung objTemp = (from b in db.tbl_NguoiDungs
                                         where b.TrangThai == true && b.ID == objDelete.ID
                                         select b).FirstOrDefault();
                if (objTemp != null)
                {
                    objTemp.TrangThai = false;
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception ex)
            {
                _logFile.Write("Exception when using NguoiDung_DAL.cs => Delete Message:" + ex.Message + " Source:" + ex.Source + " StackTrace:" + ex.StackTrace);
                return false;
            }
        }

        //Lấy tất cả danh sách người dùng
        public static List<tbl_NguoiDung> GetAll()
        {
            try
            {
                var db = new QLBHDataContext(DataService.m_ConnectString);
                List<tbl_NguoiDung> lst = (from m in db.tbl_NguoiDungs
                                           where m.TrangThai == true
                                           select m).ToList<tbl_NguoiDung>();
                return lst;
            }
            catch (Exception ex)
            {
                _logFile.Write("Exception when using NguoiDung_DAL.cs => GetAll Message:" + ex.Message + " Source:" + ex.Source + " StackTrace:" + ex.StackTrace);
                return null;
            }
        }

        public static tbl_NguoiDung SelectByID(int _IDNguoiDung)
        {
            try
            {
                var db = new QLBHDataContext(DataService.m_ConnectString);
                tbl_NguoiDung obj = (from m in db.tbl_NguoiDungs
                                     where m.TrangThai == true
                                     && m.ID == _IDNguoiDung
                                     select m).FirstOrDefault<tbl_NguoiDung>();
                return obj;
            }
            catch (Exception ex)
            {
                _logFile.Write("Exception when using NguoiDung_DAL.cs => GetAll Message:" + ex.Message + " Source:" + ex.Source + " StackTrace:" + ex.StackTrace);
                return null;
            }
        }

        /// <summary>
        /// Lấy danh sách người cùng sử dụng 1 vai trò truyền vào
        /// </summary>
        /// <param name="_VaiTroID"></param>
        /// <returns></returns>
        public static List<tbl_NguoiDung> LayNguoiDungTheoVaiTroID(long _VaiTroID)
        {
            try
            {
                var db = new QLBHDataContext(DataService.m_ConnectString);
                List<tbl_NguoiDung> lstRes = (from m in db.tbl_NguoiDungs
                                              where m.fk_VaiTro == _VaiTroID
                                              && m.TrangThai == true && m.NguoiTao == util.GetUserID()
                                              select m).ToList<tbl_NguoiDung>();
                return lstRes;
            }
            catch (Exception ex)
            {
                _logFile.Write("Exception when using NguoiDung_DAL.cs => LayNguoiDungTheoVaiTroID Message:" + ex.Message + " Source:" + ex.Source + " StackTrace:" + ex.StackTrace);
                return null;
            }
        }

        public static tbl_NguoiDung SelectByUserName(string TaiKhoan)
        {
            try
            {
                var db = new QLBHDataContext(DataService.m_ConnectString);
                tbl_NguoiDung obj = (from m in db.tbl_NguoiDungs
                                     where m.TrangThai == true
                                     && m.TaiKhoan == TaiKhoan
                                     select m).FirstOrDefault<tbl_NguoiDung>();
                return obj;
            }
            catch (Exception ex)
            {
                _logFile.Write("Exception when using NguoiDung_DAL.cs => GetAll Message:" + ex.Message + " Source:" + ex.Source + " StackTrace:" + ex.StackTrace);
                return null;
            }
        }
    }
}
